package net.lwf.hibernate.dao.worksheet;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

import net.lwf.common.constant.ICommonConstant;
import net.lwf.constant.IDalConstant;
import net.lwf.exception.ExceptionUtil;
import net.lwf.factory.lookup.LookupFactory;
import net.lwf.hibernate.dao.BaseDAO;
import net.lwf.hibernate.entity.worksheet.WorkSheetInfo;
import net.lwf.hibernate.util.HibernateUtil;
import net.lwf.security.DesEncryptor;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;


public class WorkSheetDAO extends BaseDAO {
	
	public WorkSheetInfo getWorkSheetInfo(long id) {
		
		WorkSheetInfo info = null;
		Session session = HibernateUtil.getSessionFactory().openSession();
	    try {
	    	info = (WorkSheetInfo) session.get(WorkSheetInfo.class, new Long(id));
	    }
	    catch(HibernateException hex) {
	    	ExceptionUtil.logAndThrowException(WorkSheetDAO.class, hex);
	    }
	    finally {
	    	if(session != null) {
	    		session.close();
	    	}
	    }
		return info;
	}

	public List<WorkSheetInfo> getWorkSheetInfo(String grade, String subject) {
		return getWorkSheetInfo(grade, subject, null);
	}
	
	@SuppressWarnings("unchecked")
	public List<WorkSheetInfo> getWorkSheetInfo(String grade, String subject, String topic) {
		List<WorkSheetInfo> data = null;
	    Session session = HibernateUtil.getSessionFactory().openSession();
	    try {
	    	Criteria criteria = session.createCriteria(WorkSheetInfo.class);
	    	criteria.add(Restrictions.eq("grade", grade));
	    	if(subject != null) {
	    		criteria.add(Restrictions.eq("subject", subject));
	    	}
	    	if(topic != null) {
	    		criteria.add(Restrictions.eq("topic", topic));
	    	}
	    	criteria.add((Restrictions.eq("status", "ACTIVE")).ignoreCase());
	    	data = criteria.list();
	    	
	    }
	    catch(HibernateException hex) {
	    	ExceptionUtil.logAndThrowException(WorkSheetDAO.class, hex);
	    }
	    finally {
	    	if(session != null) {
	    		session.close();
	    	}
	    }
	    this.setPrintKey(data);
		return data;
	}
	
	public int getAvailableWorkSheets(String grade) {
		return getWorkSheetInfo(grade, null, null).size();
	}
	
	private void setPrintKey(List<WorkSheetInfo> wsinfo) {
		String encryptionKey = null;
		String encryptionScheme = null;

//		if(LwfService.isMock()) {
//			MockLookupService service = new MockLookupService();
//			encryptionKey = service.getLookupValue(IDalConstant.SECURITY, IDalConstant.DESEDE_ENCRYPTION_KEY);
//			encryptionScheme = service.getLookupValue(IDalConstant.SECURITY, IDalConstant.DESEDE_ENCRYPTION_SCHEME_KEY);
//		}
//		else {
//			encryptionKey = LookupFactory.getInstance().getValue(IDalConstant.SECURITY, IDalConstant.DESEDE_ENCRYPTION_KEY);
//			encryptionScheme = LookupFactory.getInstance().getValue(IDalConstant.SECURITY, IDalConstant.DESEDE_ENCRYPTION_SCHEME_KEY);
//		}
		
		encryptionKey = LookupFactory.getInstance().getValue(IDalConstant.SECURITY, IDalConstant.DESEDE_ENCRYPTION_KEY);
		encryptionScheme = LookupFactory.getInstance().getValue(IDalConstant.SECURITY, IDalConstant.DESEDE_ENCRYPTION_SCHEME_KEY);
		
		
		DesEncryptor encryptor = null;
		try {
			encryptor = new DesEncryptor(encryptionScheme, encryptionKey);
			for(WorkSheetInfo info: wsinfo) {
				//Encrypt print key
				String key_to_encrypt = info.getPrintKey();
				//System.out.println("............ Key to encrypt: " + key_to_encrypt);
	
				String encrypted = encryptor.encrypt(key_to_encrypt);
				//System.out.println("............ Encrypted String: " + encrypted);
				String encoded = URLEncoder.encode(encrypted, ICommonConstant.CHARACTER_ENCODING);
	
				//System.out.println("............ Encoded String: " + encoded);
				info.encryptPrintKey(encoded);
			}
		} 
		catch (UnsupportedEncodingException useex) {
			ExceptionUtil.logAndThrowException("Could not encrypt the data......", WorkSheetDAO.class, useex);
		}
	}

} //WorkSheetDAO
